﻿#include "u_config_system.h"

ConfigSystem::ConfigSystem(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();
    this->OnResetClicked();

    this->setTitle("系统配置");
    this->setMaxHide();
    this->resize(500, 400);
}

void ConfigSystem::InitWidget(){

    m_SaveBut  = new QPushButton(QChar(0xe657) + QString(" 保存"));   m_SaveBut->setMinimumWidth(100);
    m_ResetBut = new QPushButton(QChar(0xe6dc) + QString(" 重置"));   m_ResetBut->setMinimumWidth(100);

    m_SerialBox     = new QComboBox();
    m_BaudRateBox   = new QComboBox();
    m_DataBitBox    = new QComboBox();
    m_CheckoutBitBox = new QComboBox();
    m_StopBitBox    = new QComboBox();

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(m_SaveBut);
    btnLayout->addWidget(m_ResetBut);

    QGridLayout *sysGridLayout = new QGridLayout();
    sysGridLayout->setSpacing(10);
    sysGridLayout->setColumnStretch(1,1);
    sysGridLayout->setColumnStretch(2,4);
    sysGridLayout->addWidget(new QLabel("串口:"),   2,1,1,1, Qt::AlignRight);
    sysGridLayout->addWidget(m_SerialBox,           2,2,1,1);
    sysGridLayout->addWidget(new QLabel("波特率:"),   3,1,1,1, Qt::AlignRight);
    sysGridLayout->addWidget(m_BaudRateBox,         3,2,1,1);
    sysGridLayout->addWidget(new QLabel("数据位:"),   4,1,1,1, Qt::AlignRight);
    sysGridLayout->addWidget(m_DataBitBox,          4,2,1,1);
    sysGridLayout->addWidget(new QLabel("校验位:"),   5,1,1,1, Qt::AlignRight);
    sysGridLayout->addWidget(m_CheckoutBitBox,      5,2,1,1);
    sysGridLayout->addWidget(new QLabel("停止位:"),   6,1,1,1, Qt::AlignRight);
    sysGridLayout->addWidget(m_StopBitBox,          6,2,1,1);

    if(globalData->CheckUserRole(0)){
        sysGridLayout->addLayout(btnLayout,             7,1,1,2);
    }

    QGroupBox *sysGroupBox = new QGroupBox("串口配置");
    sysGroupBox->setLayout(sysGridLayout);

    //-----------------------------------------------------------
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(sysGroupBox);

    this->addLayout(mainLayout);
}

void ConfigSystem::SetupConnections(){
    connect(m_ResetBut , SIGNAL(clicked()), this, SLOT(OnResetClicked()));
    connect(m_SaveBut , SIGNAL(clicked()), this, SLOT(OnSaveClicked()));
}

void ConfigSystem::Initialize(){
    m_SerialBox->addItems(this->GetSerials());
    m_BaudRateBox->addItems({"1200", "2400","4800", "9600", "19200", "38400", "57600", "115200"});
    m_DataBitBox->addItems({"5", "6", "7", "8"});
    m_CheckoutBitBox->addItems({"N空", "E偶", "0奇", "S空格", "M标志"});
    m_StopBitBox->addItems({"1", "1.5", "2"});
}

void ConfigSystem::OnResetClicked(){
    Serial *serial = globalData->config->serial;

    m_SerialBox->setCurrentText(serial->serialName);
    m_BaudRateBox->setCurrentText(QString::number(serial->baudRate));
    m_DataBitBox->setCurrentText(QString::number(serial->dataBit));
    m_CheckoutBitBox->setCurrentIndex(serial->checkoutBit);
    m_StopBitBox->setCurrentIndex(serial->stopBit);
}

void ConfigSystem::OnSaveClicked(){

    if(!MyGUI::ShowMessageBoxQuesion("确定保存吗？")){
        return;
    }

    //----------------------------------------------------------

    Serial *serial = globalData->config->serial;


    serial->serialName = m_SerialBox->currentText();
    serial->baudRate = m_BaudRateBox->currentText().toInt();
    serial->dataBit = m_DataBitBox->currentText().toInt();
    serial->checkoutBit = m_CheckoutBitBox->currentIndex();
    serial->stopBit = m_StopBitBox->currentIndex();

    globalData->config->SaveConfig();
    globalData->SaveSysLog("修改配置", 1);
    MyGUI::ShowMessageBoxInfo("保存成功");
}

QStringList ConfigSystem::GetSerials(){
    QStringList names;
    const QList<QSerialPortInfo> availablePorts = QSerialPortInfo::availablePorts();
    for (const QSerialPortInfo &info : availablePorts) {
        names.append(info.portName());
    }
    return  names;
}
